﻿@using Microsoft.AspNetCore.Mvc.Localization
@model ChargePointViewModel
@inject IViewLocalizer Localizer
@{
    ViewData["Title"] = @Localizer["Title"];
}
<br />

@if (Model != null)
{
    @using (Html.BeginForm())
    {
        @Html.AntiForgeryToken()

        <h4>@Localizer["EditChargePoint"]</h4>
        <br />
        <div class="container">
            <div class="row">
                <div class="col-sm-2 align-self-center">
                    <div class="form-group">
                        <label class="inline-label" for="ChargePointId">@Localizer["ChargePointIdLabel"]</label>
                    </div>
                </div>
                @if (Model.CurrentId == "@")
                {
                    <div class="col-sm-6">
                        <div class="form-group">
                            <input type="text" class="form-control" data-val="true" data-val-length="@string.Format(Localizer["FieldMaxLength"].Value, 100)" data-val-length-max="100" data-val-required="@Localizer["RequiredField"]" id="ChargePointId" maxlength="100" name="ChargePointId" placeholder="@Localizer["ChargePointIdPlaceholder"]" value="@Model.ChargePointId">
                        </div>
                    </div>
                    <div class="col-sm-2">
                        <span class="field-validation-valid text-danger" data-valmsg-for="ChargePointId" data-valmsg-replace="true"></span>
                    </div>
                }
                else
                {
                    <div class="col-sm-6">
                        <div class="form-group">
                            <input type="text" readonly class="form-control" id="ChargePointId" name="ChargePointId" placeholder="@Localizer["ChargePointIdPlaceholder"]" value="@Model.ChargePointId">
                        </div>
                    </div>
                }
            </div>
            <div class="row">
                <div class="col-sm-2 align-self-center">
                    <div class="form-group">
                        <label class="inline-label" for="Name">@Localizer["NameLabel"]</label>
                    </div>
                </div>
                <div class="col-sm-6">
                    <div class="form-group">
                        <input class="form-control" data-val="true" data-val-length="@string.Format(Localizer["FieldMaxLength"].Value, 100)" data-val-length-max="100" data-val-required="@Localizer["RequiredField"]" id="Name" maxlength="100" name="Name" placeholder="@Localizer["NamePlaceholder"]" type="text" value="@Model.Name" />
                    </div>
                </div>
                <div class="col-sm-2">
                    <span class="field-validation-valid text-danger" data-valmsg-for="Name" data-valmsg-replace="true"></span>
                </div>
            </div>
            <div class="row">
                <div class="col-sm-2 align-self-center">
                    <div class="form-group">
                        <label class="inline-label" for="Comment">@Localizer["CommentLabel"]</label>
                    </div>
                </div>
                <div class="col-sm-6">
                    <div class="form-group">
                        <input class="form-control" data-val="true" data-val-length="@string.Format(Localizer["FieldMaxLength"].Value, 200)" data-val-length-max="200" id="Comment" maxlength="200" name="Comment" placeholder="@Localizer["CommentPlaceholder"]" type="text" value="@Model.Comment" />
                    </div>
                </div>
                <div class="col-sm-2">
                    <span class="field-validation-valid text-danger" data-valmsg-for="Comment" data-valmsg-replace="true"></span>
                </div>
            </div>


            <div class="row">
                <div class="col-sm-2 align-self-center">
                    <div class="form-group">
                        <label class="inline-label" for="Username">@Localizer["UsernameLabel"]</label>
                    </div>
                </div>
                <div class="col-sm-6">
                    <div class="form-group">
                        <input class="form-control" data-val="true" data-val-length="@string.Format(Localizer["FieldMaxLength"].Value, 50)" data-val-length-max="50" id="Username" maxlength="50" name="Username" placeholder="@Localizer["UsernamePlaceholder"]" type="text" value="@Model.Username" />
                    </div>
                </div>
                <div class="col-sm-2">
                    <span class="field-validation-valid text-danger" data-valmsg-for="Username" data-valmsg-replace="true"></span>
                </div>
            </div>
            <div class="row">
                <div class="col-sm-2 align-self-center">
                    <div class="form-group">
                        <label class="inline-label" for="Password">@Localizer["PasswordLabel"]</label>
                    </div>
                </div>
                <div class="col-sm-6">
                    <div class="form-group">
                        <input class="form-control" data-val="true" data-val-length="@string.Format(Localizer["FieldMaxLength"].Value, 50)" data-val-length-max="50" id="Password" maxlength="50" name="Password" placeholder="@Localizer["PasswordPlaceholder"]" type="text" value="@Model.Password" />
                    </div>
                </div>
                <div class="col-sm-2">
                    <span class="field-validation-valid text-danger" data-valmsg-for="Password" data-valmsg-replace="true"></span>
                </div>
            </div>
            <div class="row">
                <div class="col-sm-2 align-self-center">
                    <div class="form-group">
                        <label class="inline-label" for="ClientCertThumb">@Localizer["ClientCertThumbLabel"]</label>
                    </div>
                </div>
                <div class="col-sm-6">
                    <div class="form-group">
                        <input class="form-control" data-val="true" data-val-length="@string.Format(Localizer["FieldMaxLength"].Value, 100)" data-val-length-max="100" id="ClientCertThumb" maxlength="100" name="ClientCertThumb" placeholder="@Localizer["ClientCertThumbPlaceholder"]" type="text" value="@Model.ClientCertThumb" />
                    </div>
                </div>
                <div class="col-sm-2">
                    <span class="field-validation-valid text-danger" data-valmsg-for="ClientCertThumb" data-valmsg-replace="true"></span>
                </div>
            </div>

            <div class="row">
                <div class="col-sm-12 text-danger">
                    @ViewBag.ErrorMsg
                </div>
            </div>
            <div class="row">
                <div class="col-sm-1">
                    &nbsp;
                </div>
            </div>
            <div class="row">
                <div class="col-sm-2">
                    <button type="submit" class="btn btn-primary">@Localizer[(Model.CurrentId == "@") ? "SaveNew" : "Save"].Value</button>
                </div>
            </div>
            @if (!string.IsNullOrWhiteSpace(Model.ChargePointId))
            {
                <div class="row">
                    <div class="col-sm-1">
                        &nbsp;
                    </div>
                </div>
                <div class="row">
                    <div class="col-sm-1">
                        &nbsp;
                    </div>
                </div>
                <div class="row">
                    <div class="col-sm-2">
                        <button type="button" class="btn btn-info" id="btnReset" title="@Localizer["TitleReset"]" onclick="ResetChargepoint()"><i class="fas fa-redo"></i> @Localizer["TitleReset"]</button>
                    </div>
                    <div class="col-sm-2">
                        <button type="button" class="btn btn-info" id="btnUnlock" title="@Localizer["TitleUnlockConnector"]" onclick="UnlockConnector()"><i class="fas fa-lock-open"></i> @Localizer["TitleUnlockConnector"]</button>
                    </div>
                </div>
            }
        </div>
    }


    @section scripts {
        @if (!string.IsNullOrWhiteSpace(Model.ChargePointId))
        {
            <script>
            function ResetChargepoint() {
                var dialog = new BootstrapDialog({
                    title: '@Localizer["TitleReset"]',
                    message: '@string.Format(Localizer["DialogReset"].Value, Model.Name)',
                    spinicon: 'fa fa-spinner fa-fw',
                    buttons: [{
                        id: 'btnDialogReset',
                        label: '@Localizer["TitleReset"]',
                        icon: 'fas fa-redo',
                        autospin: true,
                        action: function (dialogRef) {
                            dialogRef.enableButtons(false);
                            dialogRef.setClosable(false);
                            dialogRef.getModalBody().html('@Localizer["ExecuteReset"]');

                            var xmlhttp = new XMLHttpRequest();
                            xmlhttp.onreadystatechange = function () {
                                if (xmlhttp.readyState == XMLHttpRequest.DONE) {
                                    if (xmlhttp.status == 200) {
                                        dialogRef.getModalBody().html(xmlhttp.responseText);
                                    }
                                    else {
                                        dialogRef.getModalBody().html('@Localizer["ErrorReset"]');
                                    }

                                    dialogRef.setClosable(true);
                                    dialogRef.enableButtons(true);
                                    var $resetButton = dialog.getButton('btnDialogReset');
                                    $resetButton.hide();
                                    var $cancelButton = dialog.getButton('btnDialogCancel');
                                    $cancelButton.text('@Localizer["Close"]');

                                }
                            };
                            xmlhttp.open("GET", "@Html.Raw(Url.Content("~/API/Reset/" + Uri.EscapeDataString(Model.ChargePointId)))", true);
                            xmlhttp.send();
                        }
                    }, {
                        id: 'btnDialogCancel',
                        label: '@Localizer["Cancel"]',
                        action: function (dialogRef) {
                            dialogRef.close();
                        }
                    }]
                });
                dialog.open();
            }

            function UnlockConnector() {
                var dialog = new BootstrapDialog({
                    title: '@Localizer["TitleUnlockConnector"]',
                    message: '@string.Format(Localizer["DialogUnlockConnector"].Value, Model.Name)',
                    spinicon: 'fa fa-spinner fa-fw',
                    buttons: [{
                        id: 'btnUnlock',
                        label: '@Localizer["TitleUnlockConnector"]',
                        icon: 'fas fa-lock-open',
                        autospin: true,
                        action: function (dialogRef) {
                            dialogRef.enableButtons(false);
                            dialogRef.setClosable(false);
                            dialogRef.getModalBody().html('@Localizer["ExecuteUnlock"]');

                            var xmlhttp = new XMLHttpRequest();
                            xmlhttp.onreadystatechange = function () {
                                if (xmlhttp.readyState == XMLHttpRequest.DONE) {
                                    if (xmlhttp.status == 200) {
                                        dialogRef.getModalBody().html(xmlhttp.responseText);
                                    }
                                    else {
                                        dialogRef.getModalBody().html('@Localizer["ErrorUnlock"]');
                                    }

                                    dialogRef.setClosable(true);
                                    dialogRef.enableButtons(true);
                                    var $resetButton = dialog.getButton('btnUnlock');
                                    $resetButton.hide();
                                    var $cancelButton = dialog.getButton('btnDialogCancel');
                                    $cancelButton.text('@Localizer["Close"]');

                                }
                            };
                                xmlhttp.open("GET", "@Html.Raw(Url.Content("~/API/UnlockConnector/" + Uri.EscapeDataString(Model.ChargePointId)))", true);
                            xmlhttp.send();
                        }
                    }, {
                        id: 'btnDialogCancel',
                        label: '@Localizer["Cancel"]',
                        action: function (dialogRef) {
                            dialogRef.close();
                        }
                    }]
                });
                dialog.open();
            }
            </script>
        }
    }
}